package com.xinxin.leetcode.problem414;

/**
 * @author ShiXinXin
 * @date 2019-12-16 10:52
 */
public class Solution {
    public int thirdMax(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int num1 = Integer.MIN_VALUE, num2 = Integer.MIN_VALUE, num3 = Integer.MIN_VALUE;
        int sign = 0;
        for (int num : nums) {
            if (num == num1 || num == num2 || num == num3) {
                continue;
            }
            if (num > num1) {
                num3 = num2;
                num2 = num1;
                num1 = num;
                sign++;
            } else if (num > num2) {
                num3 = num2;
                num2 = num;
                sign++;
            } else if (num > num3) {
                num3 = num;
                sign++;
            }
        }
        return sign >= 3 ? num3 : num1;
    }

    public static void main(String[] args) {
        int[] arr = {2, 2, 3, 1};
        System.out.println(new Solution().thirdMax(arr));
    }
}
